#include "config.h"
#include "stc32g.h"
#include "STC32G_Delay.h"
#include "STC32G_UART.h"
#include "STC32G_NVIC.h"
#include "STC32G_Switch.h"
#include "STC32G_I2C.h"
#include "STC32G_ADC.h"
#include "TM1650.h"
#include "RX8010.h"
#include "NMEA.h"
#include "STC32G_Exti.h"
#include <string.h>
#include "stc32_stc8_usb.h"
#define DOT P34
#define LED P23

char *USER_DEVICEDESC = NULL;
char *USER_PRODUCTDESC = NULL;
char *USER_STCISPCMD = "@STCISP#";
extern int end;
extern BOOL UART2_Receive_end;
extern char receive[128];
extern float cog, kn, km;
extern char time[10];
extern u8 WakeUpSource;
float speed[20];
u8 function = 0;
int dim = 0;
void UART_config()
{
    COMx_InitDefine COMx_InitStructure; // �ṹ����

    COMx_InitStructure.UART_Mode = UART_8bit_BRTx; // ģʽ,   UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
    //	COMx_InitStructure.UART_BRT_Use   = BRT_Timer2;			//ѡ�����ʷ�����, BRT_Timer2 (ע��: ����2�̶�ʹ��BRT_Timer2, ���Բ���ѡ��)
    COMx_InitStructure.UART_BaudRate = 9600ul;      // ������,     110 ~ 115200
    COMx_InitStructure.UART_RxEnable = ENABLE;      // ��������,   ENABLE��DISABLE
    UART_Configuration(UART2, &COMx_InitStructure); // ��ʼ������2 USART1,USART2,USART3,USART4
    NVIC_UART2_Init(ENABLE, Priority_1);            // �ж�ʹ��, ENABLE/DISABLE; ���ȼ�(�͵���) Priority_0,Priority_1,Priority_2,Priority_3
                                                    //   UART2_SW(UART2_SW_P10_P11);                     // UART2_SW_P10_P11,UART2_SW_P46_P47

    /*    COMx_InitStructure.UART_Mode = UART_8bit_BRTx;  // ģʽ, UART_ShiftRight,UART_8bit_BRTx,UART_9bit,UART_9bit_BRTx
       COMx_InitStructure.UART_BRT_Use = BRT_Timer1;   // ѡ�����ʷ�����, BRT_Timer1, BRT_Timer2 (ע��: ����2�̶�ʹ��BRT_Timer2)
       COMx_InitStructure.UART_BaudRate = 115200ul;    // ������, һ�� 110 ~ 115200
       COMx_InitStructure.UART_RxEnable = ENABLE;      // ��������,   ENABLE��DISABLE
       COMx_InitStructure.BaudRateDouble = DISABLE;    // �����ʼӱ�, ENABLE��DISABLE
       UART_Configuration(UART1, &COMx_InitStructure); // ��ʼ������1 UART1,UART2,UART3,UART4
       NVIC_UART1_Init(ENABLE, Priority_1);            // �ж�ʹ��, ENABLE/DISABLE; ���ȼ�(�͵���) Priority_0,Priority_1,Priority_2,Priority_3

       UART1_SW(UART1_SW_P30_P31); // UART1_SW_P30_P31,UART1_SW_P36_P37,UART1_SW_P16_P17,UART1_SW_P43_P44 */
}
void ADC_config(void)
{
    ADC_InitTypeDef ADC_InitStructure;                     // �ṹ����
    ADC_InitStructure.ADC_SMPduty = 31;                    // ADC ģ���źŲ���ʱ�����?, 0~31��ע�⣺ SMPDUTY һ����������С�� 10��
    ADC_InitStructure.ADC_CsSetup = 0;                     // ADC ͨ��ѡ��ʱ�����? 0(Ĭ��),1
    ADC_InitStructure.ADC_CsHold = 1;                      // ADC ͨ��ѡ�񱣳�ʱ�����? 0,1(Ĭ��),2,3
    ADC_InitStructure.ADC_Speed = ADC_SPEED_2X16T;         // ���� ADC ����ʱ��Ƶ��	ADC_SPEED_2X1T~ADC_SPEED_2X16T
    ADC_InitStructure.ADC_AdjResult = ADC_RIGHT_JUSTIFIED; // ADC�������?,	ADC_LEFT_JUSTIFIED,ADC_RIGHT_JUSTIFIED
    ADC_Inilize(&ADC_InitStructure);                       // ��ʼ��
    ADC_PowerControl(ENABLE);                              // ADC��Դ����, ENABLE��DISABLE
    //    NVIC_ADC_Init(DISABLE, Priority_0);                    // �ж�ʹ��, ENABLE/DISABLE; ���ȼ�(�͵���) Priority_0,Priority_1,Priority_2,Priority_3
}
void I2C_config(void)
{
    I2C_InitTypeDef I2C_InitStructure;

    I2C_InitStructure.I2C_Mode = I2C_Mode_Master; // ����ѡ��   I2C_Mode_Master, I2C_Mode_Slave
    I2C_InitStructure.I2C_Enable = ENABLE;        // I2C����ʹ��,   ENABLE, DISABLE
    I2C_InitStructure.I2C_MS_WDTA = DISABLE;      // ����ʹ���Զ�����,  ENABLE, DISABLE
    I2C_InitStructure.I2C_Speed = 20;             // �����ٶ�=Fosc/2/(Speed*2+4),      0~63
    I2C_Init(&I2C_InitStructure);
    //   NVIC_I2C_Init(I2C_Mode_Master, DISABLE, Priority_0); // ����ģʽ, I2C_Mode_Master, I2C_Mode_Slave; �ж�ʹ��, ENABLE/DISABLE; ���ȼ�(�͵���) Priority_0,Priority_1,Priority_2,Priority_3
}
void Exti_config(void)
{
    EXTI_InitTypeDef Exti_InitStructure; // �ṹ����

    Exti_InitStructure.EXTI_Mode = EXT_MODE_RiseFall; // �ж�ģʽ,   EXT_MODE_RiseFall,EXT_MODE_Fall
    Ext_Inilize(EXT_INT0, &Exti_InitStructure);       // ��ʼ��
    NVIC_INT0_Init(ENABLE, Priority_0);               // �ж�ʹ��, ENABLE/DISABLE; ���ȼ�(�͵���) Priority_0,Priority_1,Priority_2,Priority_3
}
void stcinit()
{
    WTST = 0;  // ���ó���ָ����ʱ��������ֵΪ0�ɽ�CPUִ��ָ����ٶ������?���?
    EAXSFR();  // ��չSFR(XFR)����ʹ��
    CKCON = 0; // ��߷���XRAM�ٶ�
    P_SW2 |= 0x80;
    P0M0 = 0x00;
    P0M1 = 0x00;

    P1M1 = 0x00;
    P1M0 = 0x00;
    P2M0 = 0x08;
    P2M1 = 0x08;

    P3M1 = 0x00;
    P3M0 = 0x00;
    P4M1 = 0x00;
    P4M0 = 0x00;
    P5M1 = 0x00;
    P5M0 = 0x00;
    P6M1 = 0x00;
    P6M0 = 0x00;
    P7M1 = 0x00;
    P7M0 = 0x00;
}

void main()
{
    int led = 0;

    struct DateTime datetime = {1, 1, 1, 1, 1, 1};
    GpsData gt;
    BOOL gpsUpdate = 0;
    u8 quotatoin;

    stcinit();
    LED = 0;
    UART_config();
    IRC48MCR = 0X80;
    // while (!(IRC48MCR & 0x01))
    ;
    USBCLK = 0x00;
    USBCON = 0x90;
    usb_init(); // USB CDC �ӿ�����

    IE2 |= 0x80; // ʹ��USB�ж�
    // ʹ��P2�ж�
    P2IM0 = 0x00;
    P2IM1 = 0x00;
    P2INTE = 0xff;
    EA = 1;
    I2C_config();
    ADC_config();
    //    while (DeviceState != DEVSTATE_CONFIGURED)
    ; // �ȴ�USB�������? */
    printf("test\n");
    //   delay_ms(1000);
    DOT = 1;

    //    PrintString1("STC32G begin!\r\n");
    resetRX8010();
    //   setTime(&dt);
    setFOUT(IRQ1, 1);
    Exti_config();
    getDateTime(&datetime);
    if (datetime.second % 2 == 0)
        quotatoin = 2;
    PrintString2("$PCAS03,1,0,0,0,0,1,1,0,0,0,,,0,0*03\r\n"); // NMEA output GGA VTG ZDA
    PrintString2("$PCAS02,500*1A\r\n");                       // NMEA output 2Hz

    while (1)
    {
        //   delay_ms(1);
        if (bUsbOutReady)
        {
            USB_SendData(UsbOutBuffer, OutNumber); // �������ݻ����������ȣ���������ԭ������, ���ڲ��ԣ�

            usb_OUT_done();
        }
        if (COM2.RX_TimeOut == 1) // ��ʱ����测试
        {

            /*   if (receive[3] == 'G' & receive[4] == 'G' & receive[5] == 'A')
             */
            nmeaZDA(receive, &gt);
            //    if (nmeaZDA(receive, &gt) == 1 /* & gpsUpdate == 0*/)

            if (gt.HDOP > 10 | gt.GPSStatus == 0 | gt.GPSStatus > 2)
            {
                gt.cog = 1000;
                gt.kn = 1000;
                gt.km = 1000;
            }
            COM2.RX_TimeOut = 0;
        }
        if (WakeUpSource == 1)
        {
            getDateTime(&datetime);
            if (abs((datetime.minute * 60 + datetime.second) - (gt.minute * 60 + gt.second)) > 1 & gt.year > 0)
            {
                datetime.year = gt.year;
                datetime.month = gt.month;
                datetime.date = gt.date;
                datetime.hour = gt.hour;
                datetime.minute = gt.minute;
                datetime.second = gt.second;
                setTime(&datetime);
                printf("------Time sync OK!------\n");
            }
            switch (function)
            {
            case 0:
            {
                if (quotatoin == 0)
                {
                    quotatoin = 2;
                    LED = 0;
                    printf("F0 LT:20%02d-%02d-%02d %02d:%02d:%02d GT:20%02d-%02d-%02d %02d:%02d:%02d\n", datetime.year, datetime.month, datetime.date, datetime.hour, datetime.minute, datetime.second, gt.year, gt.month, gt.date, gt.hour, gt.minute, gt.second);
                }
                else
                {
                    quotatoin = 0;
                    LED = 1;
                }
                DOT = 1;
                TM1650Display(datetime.hour * 100 + datetime.minute, quotatoin, Get_ADCResult(8) / 500 + dim, 1);
                led = 0;
            }
            break;
            case 1:
            {
                DOT = 0;
                TM1650Display(gt.km * 10, 3, Get_ADCResult(8) / 500 + dim, 0);
                printf("F1 GT:20%02d-%02d-%02d %02d:%02d:%02d COG:%.1f KN:%.1f KM:%.1f dim=%d GPS:%d Height:%.2fm HDOP=%.1f\n", gt.year, gt.month, gt.date, gt.hour, gt.minute, gt.second, gt.cog, gt.kn, gt.km, dim, gt.GPSStatus, gt.Height, gt.HDOP);
            }
            break;
            case 2:
            {
                DOT = 0;
                TM1650Display(gt.cog * 10, 3, Get_ADCResult(8) / 500 + dim, 1);
                printf("F2 GT:20%02d-%02d-%02d %02d:%02d:%02d COG:%.1f KN:%.1f KM:%.1f dim=%d GPS:%d Height:%.2fm HDOP=%.1f\n", gt.year, gt.month, gt.date, gt.hour, gt.minute, gt.second, gt.cog, gt.kn, gt.km, dim, gt.GPSStatus, gt.Height, gt.HDOP);
            }
            break;
            default:
                break;
            }

            WakeUpSource = 0;
        }
    }
}

void p2Int() interrupt 39
{
    u8 intf;
    intf = P2INTF;
    if (intf)
    {
        P2INTF = 0x00;
        if (intf & 0x01) // SET
        {
            function = (++function) % 3;
            dim = 0;
        }
        if (intf & 0x02) // ADD
        {
            dim += 1;
            if (dim > 8)
                dim = 8;
        }
        if (intf & 0x04) // SUB
        {
            dim -= 1;
            if (dim < -7)
                dim = -7;
        }
    }
}